
package model;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;

public class GPS {
	
	private ArrayList<GGA> ggas = new ArrayList<GGA>();
	private ArrayList<RMC> rmcs= new ArrayList<RMC>();	

	/**
	 * Carrega el ficher de dades.
	 * @return true si s'ha carregat, false si no l'ha carregat.
	 */
	public boolean loadFile(String file) {
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
		    try {
		    	ggas = new ArrayList<GGA>();
		    	rmcs= new ArrayList<RMC>();	
		        String line = br.readLine();
		        boolean contador = true;
		        
		        while (line != null) {
		            String data[] = line.split(",");
		            GGA ggaAux = new GGA();
		            RMC rmcAux = new RMC();
		            
		            if (contador){
		            	ggaAux.setTime(data[1]);
		            	
		            	float grau = Float.parseFloat(data[2].substring(0,2));
		            	float minuts = Float.parseFloat(data[2].substring(2,data[2].length()));
		            	
		            	ggaAux.setLatitude(new Coordenada(grau, minuts, data[3]));
		            	
		            	grau = Float.parseFloat(data[4].substring(0,3));
		            	minuts = Float.parseFloat(data[4].substring(3,data[4].length()));
		            	
		            	ggaAux.setLongitude(new Coordenada(grau, minuts, data[5]));
		            	
		            	ggaAux.setQuality(Integer.parseInt(data[6]));
		            	ggaAux.setSatellites(Integer.parseInt(data[7]));
		            	
		            	if (!data[8].equals("")) {
		            		ggaAux.sethDilution(Float.parseFloat(data[8]));
		            	}
		            	ggaAux.setAltitude(data[9] + data[10]);
		            	ggaAux.sethGeoid(data[11] + data[12]);
		            	if (!data[13].equals("") && !data[14].equals("")) {
			            	ggaAux.setLastUpdate(Integer.parseInt(data[13]));
			            	//ggaAux.setIDNumber(Integer.parseInt(data[14]));
		            	}
		            	ggaAux.setChecksum(data[14]);
		            	ggas.add(ggaAux);
		            } else {
		            	rmcAux.setTime(data[1]);
		            	rmcAux.setStatus(data[2]);
		            	
		            	float grau = Float.parseFloat(data[3].substring(0,2));
		            	float minuts = Float.parseFloat(data[3].substring(2,data[3].length()));
		            	
		            	rmcAux.setLatitude(new Coordenada(grau, minuts, data[4]));
		            	
		            	grau = Float.parseFloat(data[5].substring(0,3));
		            	minuts = Float.parseFloat(data[5].substring(3,data[5].length()));
		            	
		            	rmcAux.setLongitude(new Coordenada(grau, minuts, data[6]));
		            	rmcAux.setSpeed(Float.parseFloat(data[7]));
		            	rmcAux.setAngle(Float.parseFloat(data[8]));
		            	rmcAux.setDate(data[9]);
		            	
		            	if (!data[10].equals("")){
		            		grau = Float.parseFloat(data[10]);
		            		rmcAux.setMagVar(new Coordenada(grau, 0, data[11]));
		            	}
		            	
		            	rmcAux.setChecksum(data[12]);
		            	
		            	rmcs.add(rmcAux);
		            }
		            contador = !contador;
		            line = br.readLine();
		        }
		    } finally {
		        br.close();
		    }

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}

	/**
	 * @return the ggas
	 */
	public ArrayList<GGA> getGgas() {
		return ggas;
	}

	/**
	 * @param ggas the ggas to set
	 */
	public void setGgas(ArrayList<GGA> ggas) {
		this.ggas = ggas;
	}

	/**
	 * @return the rmcs
	 */
	public ArrayList<RMC> getRmcs() {
		return rmcs;
	}

	/**
	 * @param rmcs the rmcs to set
	 */
	public void setRmcs(ArrayList<RMC> rmcs) {
		this.rmcs = rmcs;
	}
	
	
}
